package org.pine.controller;

import lombok.extern.slf4j.Slf4j;
import org.pine.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BookController2
 * @Description: 测试多数据源的请求
 * @Author liucs
 * @Date 2021-01-28
 * @Version V1.0
 */
@RestController
@Slf4j
public class BookController2 {
    @Resource(name = "jdbcTemplateOne")
    JdbcTemplate jdbcTemplateOne;
    JdbcTemplate jdbcTemplateTwo;

    @Autowired
    @Qualifier("jdbcTemplateTwo")
    public void setJdbcTemplateTwo(JdbcTemplate jdbcTemplateTwo) {
        this.jdbcTemplateTwo = jdbcTemplateTwo;
    }

    @GetMapping("/testJdbc")
    public void test(){
        List<Book> list1=jdbcTemplateOne.query("select id,name,author from book",new BeanPropertyRowMapper<>(Book.class));
        log.info("get book by jdbcTemplateOne:{}",list1);

        List<Book> list2=jdbcTemplateTwo.query("select id,book_name as name,author,price from t_book",new BeanPropertyRowMapper<>(Book.class));
        log.info("get book by jdbcTemplateTwo:{}",list2);
    }
}
